package com.fas.core;

import java.util.HashMap;
import java.util.Map;

import com.fas.hehe.system.model.LoginUser;
import com.jfinal.core.Controller;
import com.jfinal.log.Logger;
/**
 * 
 * @Copyright: Copyright (c) 2015 Knight 
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2015年10月28日 下午3:17:12
 * @version V1.0
 */
public abstract class Service {
        protected static Logger log = Logger.getLogger(Service.class);
        protected Controller controller;
        private static Map<Class<? extends Service>, Service> INSTANCE_MAP = new HashMap<Class<? extends Service>, Service>();

        @SuppressWarnings("unchecked")
        public static <SubService extends Service> SubService getInstance(Class<SubService> clazz, Controller controller) {
                SubService service = (SubService) INSTANCE_MAP.get(clazz);
                if (service == null) {
                        try {
                                // service = clazz.newInstance();
                                service = controller.enhance(clazz);
                                INSTANCE_MAP.put(clazz, service);
                        } catch (Exception e) {
                                log.error("", e);
                        }
                }
                service.controller = controller;
                return service;
        }

        public LoginUser getLoginUser() {
            //String username = (String)SecurityUtils.getSubject().getPrincipal();
            //request.setAttribute(Constants.CURRENT_USER, userService.findByUsername(username));
        	return null;
        }
}
